using UnityEngine;
using QFramework;
using System.Collections.Generic;

namespace ProjectSurvivor
{
    public partial class SimpleRotateSward : ViewController
    {
        List<Collider2D> mSwards = new List<Collider2D>();
        float radius = Global.RotateSwardRange.Value;
        void Start()
        {
            // Code Here
            void CreateSward()
            {
                mSwards.Add(Sward.InstantiateWithParent(this)
                        .Self(self =>
                        {
                            self.OnTriggerEnter2DEvent((collider) =>
                            {
                                var hurtBox = collider.GetComponent<HurtAndHitBox>();
                                if (hurtBox)
                                {
                                    if (hurtBox.Owner.CompareTag("Enemy") || hurtBox.Owner.CompareTag("EnemyFly"))
                                    {
                                        var damageTimes = Global.SuperRotateSward.Value ? Random.Range(2, 3 + 1) : 1;
                                        DemageSystem.CalculateDemage(Global.RotateSwardDemage.Value * damageTimes, hurtBox.Owner.GetComponent<IEnemy>());
                                        if (UnityEngine.Random.Range(0, 1.0f) < 0.5f)
                                        {
                                            collider.attachedRigidbody.velocity = collider.NormalizedDirection2DFrom(self) * 5 + collider.NormalizedDirection2DFrom(Player.Default) * 10;
                                        }
                                    }
                                }
                            }).UnRegisterWhenGameObjectDestroyed(self);
                        })
                        .Show());
            }

            void CreateSwards()
            {
                var toaddCount = Global.RotateSwardCount.Value + Global.AdditionFlyThingCount.Value - mSwards.Count;
                for (global::System.Int32 i = 0; i < toaddCount; i++)
                {
                    CreateSward();
                }
                UpDateCirclePos();
            }

            Global.RotateSwardCount.Or(Global.AdditionFlyThingCount).Register(() =>
            {
                CreateSwards();

                
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            CreateSwards();
            Global.RotateSwardRange.RegisterWithInitValue((range) =>
            {
                UpDateCirclePos();
            });
            Sward.Hide();

           
        }

        void UpDateCirclePos()
        {
            var durationDegrees = 360 / mSwards.Count;
            for (int i = 0; i < mSwards.Count; i++)
            {
                var circleLocalPos = new Vector2(Mathf.Cos(durationDegrees * i * Mathf.Deg2Rad), Mathf.Sin(durationDegrees * i * Mathf.Deg2Rad)) * radius;
                mSwards[i].LocalPosition(circleLocalPos.x, circleLocalPos.y).LocalEulerAnglesZ(durationDegrees * i - 90);
            }

        }
        private void Update()
        {
            var speedTimes = Global.SuperRotateSward.Value ? Time.frameCount * 10 : Global.RotateSwardSpeed.Value * Time.frameCount;

            this.LocalEulerAnglesZ(-speedTimes);

        }
    }
}
